package com.develop.utils;

import com.google.common.collect.ImmutableMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.github.kevinsawicki.http.HttpRequest;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;

public class HttpUtils {

    private static final Logger log = LoggerFactory.getLogger(HttpUtils.class);

    public static String getReturn(String url, String headerParams)
    {
        try {
            StringBuffer buffer = new StringBuffer();
            URL serverUrl = new URL(url);
            HttpURLConnection conn = (HttpURLConnection)serverUrl.openConnection();
            conn.setRequestMethod("GET");

            conn.setInstanceFollowRedirects(false);
            conn.setRequestProperty("Authorization", headerParams);
            conn.connect();

            InputStream inputStream = conn.getInputStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String str = null;
            while ((str = bufferedReader.readLine()) != null) {
                buffer.append(str);
            }
            bufferedReader.close();
            inputStreamReader.close();

            inputStream.close();
            inputStream = null;
            String result = buffer.toString();
            return result; } catch (Exception e) {
        }
        return null;
    }

    public static String executePost(String url, String json)
    {
        String result = "fail";
        try {
            HttpRequest httpRequest = HttpRequest.post(url).headers(ImmutableMap.of("Content-Type", "application/json"));
            httpRequest.trustAllCerts();
            httpRequest.trustAllHosts();
            httpRequest.send(json);
            result = httpRequest.body();
        }
        catch (Exception localException) {
        }
        return result;
    }

    public static String sendGet(String url, String queryString)
    {
        url = url + "?" + queryString;
        String timeStamp = Long.toString(new Date().getTime());
        String key = "bigapp";
        String secret = "123456";
        String strToSign = SecurityUtils.getSignString(queryString, "GET", timeStamp);
        String signatureCalc = SecurityUtils.signWithHmac(strToSign, secret);
        String authorization = "appId=" + key + ",time=" + timeStamp + ",sign=" + signatureCalc;
        String result = getReturn(url, authorization);
        System.out.println(result);
        return result;
    }

    public static String connection(String url, String submitMethod, String queryString, String dataFormat)
    {
        try
        {
            StringBuffer result = new StringBuffer();
            URL uri = new URL(url);
            HttpURLConnection urlConnection = (HttpURLConnection)uri.openConnection();
            urlConnection.setRequestProperty("content-type", dataFormat);
            urlConnection.setDoOutput(true);
            urlConnection.setDoInput(true);
            urlConnection.setRequestMethod(submitMethod);
            PrintWriter printWriter = new PrintWriter(urlConnection.getOutputStream());
            printWriter.print(queryString);
            printWriter.flush();
            urlConnection.connect();
            InputStream in = urlConnection.getInputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));
            String line="";
            while ((line = bufferedReader.readLine()) != null)
            {
                result.append(line);
            }
            bufferedReader.close();
            return result.toString();
        }
             catch (Exception e) {
            return e.toString();
        }

    }
}
